package com.neusoft.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.neusoft.common.Const;


/**
 * 
 * Created By YZ at Jun 30, 2010 11:38:37 AM
 * Class:SercurityFilter
 *
 */
public class SercurityFilter implements Filter {
	private static final Logger logger = Logger.getLogger(SercurityFilter.class);

	
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		HttpServletResponse res = (HttpServletResponse) response;
		HttpServletRequest req = (HttpServletRequest) request;
		String rqUri = req.getRequestURI();
		
		
		Map<String, String> notFilter=new HashMap<String, String>();
		//notFilter.put("users_deskTop.action", "users_deskTop.action");
		notFilter.put("task_login.action", "task_login.action");
		notFilter.put("task_loginIn.action", "task_loginIn.action");
		//notFilter.put("webService_processRequest.action", "webService_processRequest.action");
		//notFilter.put("task_init.action", "task_init.action");
		
		
		int i = rqUri.lastIndexOf("/");
		if (i != -1) {
			rqUri = rqUri.substring(i + 1);
		}
       if(notFilter.get(rqUri)!=null){
        	filterChain.doFilter(request, response);
        }else{
			if (isLogon(req)) {
				if (isAccessable(req, res)) {
					filterChain.doFilter(request, response);
				} else {
					logger.info(String.format("用户没有权限访问该连接(%s)!", req
							.getRequestURL().toString()));
	
				}
			} else {
				logger.info("你还没登陆，请先登录");
				Const.printScript((HttpServletResponse) response, "<script>var notLogonIn=true;alert('您还没登陆，请先登录');window.parent.location.href='/videoTask/task/task_login.action';</script>");
			}
        }

	}

	private boolean isLogon(HttpServletRequest request) {
		
		return request.getSession().getAttribute(Const.SESSION_TASK_INFO) != null;
	}

	/**
	 * 验证权限
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	private boolean isAccessable(HttpServletRequest request,
			HttpServletResponse response) {

		return true;
	}

	public void destroy() {
	}
}
